var ctx;
var name = "";
var type = 0;
var goodsType;
var state;

function setCtx(ctx){
	this.ctx = ctx;
}
function gotoPage(page){
	goodsType = $("#goodsType").val();
	state = $("#state").val();
	name = ifNull($("#name").val());
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if(name == $("#name").attr("placeholder")){
		name = "";
	}
	loadContent("/goodsInfo/goodsInfoList?pNo="+ page +"&name="+ name + "&state=" +state + "&goodsType=" + goodsType + "&pSize="+ pSize);
}

function ifNull(mess){
	if(mess == null || mess == undefined){
		mess = "";
	}
	return mess;
}

//条件搜索用户列表
function getGoodsInfoList(){
	goodsType = $("#goodsType").val();
	state = $("#state").val();
	name = ifNull($("#name").val());
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if(name == $("#name").attr("placeholder")){
		name = "";
	}
	loadContent("/goodsInfo/goodsInfoList?pNo=1&name="+ name + "&state=" +state + "&goodsType=" + goodsType + "&pSize="+ pSize);
	
}

function gotoGoodsInfoPage(page) {
	if(page == null){
		page = $(".inputclass").val();
	}
	goodsType = $("#goodsType").val();
	state = $("#state").val();
	name = ifNull($("#name").val());
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if(name == $("#name").attr("placeholder")){
		name = "";
	}
	loadContent("/goodsInfo/goodsInfoList?pNo="+ page +"&name="+ name  + "&state=" +state + "&goodsType=" + goodsType + "&pSize="+ pSize);
}

//新增或编辑用户(1:新增 2:编辑)
function saveOrUpdate (id) {
	var goodsType = $("#goodsType").val();
	if(id != null){

		loadContent("/goodsInfo/editGoodsInfo?id="+id+"&goodsType=" + goodsType);
		
	}else{
		loadContent("/goodsInfo/editGoodsInfo?id=0"+"&goodsType=" + goodsType);
	}
	
}


//全选和反选
function selectAll(target){    
    if(target.checked){    
        $("#tableTbody :checkbox").attr("checked", true); 
        $(target).parents("thead").siblings("tbody").children().children().each(function(){
    	    $(this).css("background","#D3F0F1");
       });
    }else{    
        $("#tableTbody :checkbox").attr("checked", false); 
        $(target).parents("thead").siblings("tbody").children().children().each(function(){
    	    $(this).css("background","");
       });
    }    
}

//鼠标滑入一行改变背景颜色
function mouseoverOne(target){
	 $(target).children().each(function(){
		  	    $(this).css("background","#D3F0F1");
		     });
}

//鼠标滑出一行恢复原来的背景颜色
function mouseoutOne(target){
	 $(target).children().each(function(){
	  	    $(this).css("background","");
	     });
}

//选择或取消选中当前记录
function selectOne(id,target){
    if(target.checked){    
        $("#goods" + id).attr("checked", true);   
        $(target).parents("tr").children().each(function(){
    	    $(this).css("background","#D3F0F1");
       });
    }else{    
        $("#goods" + id).attr("checked", false); 
        $(target).parents("tr").children().each(function(){
    	    $(this).css("background","");
       });
    } 
}

function updateState(id,state){
	var mess = "";
	if(state == 1){
		mess ="请确认上架该商品!"; 
	}else{
		mess ="请确认下架该商品"; 
	}
	 layer.confirm(mess, {icon: 3, title:'提示'}, function(index){
		    //do something
		   		$.get(ctx + "/goodsInfo/updateState",{
					id: id,
					state: state	
		   		},function(data){
	   			data = JSON.parse(data);
				if(data.success){
					layer.msg("操作成功",{
					    icon: 1,
					    time: 2000 //2秒关闭（如果不配置，默认是3秒）
					},function(){
						loadContent("/goodsInfo/goodsInfoList?goodsType=" + data.obj.type);
					});
				}else{
					layer.msg(data.msg,{
					    icon: 2,
					    time: 2000 //2秒关闭（如果不配置，默认是3秒）
					});
				}
			});
		    layer.close(index);
		}); 
} 


function deleteByIds(){
	var goodsType = $("#goodsType").val();
	if($("img[name='goodsChecked'][src*=honggou]").length == 0){
		layer.msg("请选择要删除的记录!",{
			icon: 3,
			time: 2000 //2秒关闭（如果不配置，默认是3秒）
		});
	}else{
		var delIds = [];
		    $("#tableTbody img[name='goodsChecked'][src*=honggou]").each(function(){ 
		    	delIds.push($(this).attr("class")); 
		    }); 
		    var vals = delIds.join(',');//转换为逗号隔开的字符串 
			layer.confirm('确认删除?', {icon: 3, title:'提示'}, function(index){
			    //do something
				$.post(ctx+"/goodsInfo/deleteGoodsInfo",{
					goodsIDs:vals
				},function(data){
					data = JSON.parse(data);
					if(data.success){
						layer.msg("删除成功",{
							icon: 1,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						},function(){
							loadContent("/goodsInfo/goodsInfoList?goodsType=" + goodsType);
						});
					} else {
						layer.msg(data.msg,{
							icon: 2,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						});
					}
				});
			    layer.close(index);
			});

		
	}
}

function updateSort(goodsId){
	var goodsType = $("#goodsType").val();
	if(goodsId != null){
   		$.get(ctx + "/goodsInfo/updateSort",{
			id: goodsId
   		},function(data){
		data = JSON.parse(data);
		if(data.success){
			layer.msg("操作成功",{
			    icon: 1,
			    time: 2000 //2秒关闭（如果不配置，默认是3秒）
			},function(){
				loadContent("/goodsInfo/goodsInfoList?goodsType=" + goodsType);
			});
		}else{
			layer.msg(data.msg,{
			    icon: 2,
			    time: 2000 //2秒关闭（如果不配置，默认是3秒）
			});
		}
	});
	}
}

function showDetail(id) {
    loadContent("/goodsInfo/showDetail?id=" + id);
}
